package org.dromara.business.service.impl;

import org.dromara.business.domain.Video;
import org.dromara.business.mapper.VideoMapper;
import org.dromara.business.sensitive.SensitiveWordFilter;
import org.dromara.business.service.IVideoService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.VideoDanmakuBo;
import org.dromara.business.domain.vo.VideoDanmakuVo;
import org.dromara.business.domain.VideoDanmaku;
import org.dromara.business.mapper.VideoDanmakuMapper;
import org.dromara.business.service.IVideoDanmakuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频弹幕Service业务层处理
 *
 * @author Hao Tong Xue
 * @date 2025-04-21
 */
@RequiredArgsConstructor
@Service
public class VideoDanmakuServiceImpl implements IVideoDanmakuService {

    private final VideoDanmakuMapper baseMapper;

    private final SensitiveWordFilter sensitiveWordFilter;

    private final VideoMapper videoMapper;

    /**
     * 查询视频弹幕
     *
     * @param id 主键
     * @return 视频弹幕
     */
    @Override
    public VideoDanmakuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询视频弹幕列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 视频弹幕分页列表
     */
    @Override
    public TableDataInfo<VideoDanmakuVo> queryPageList(VideoDanmakuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VideoDanmaku> lqw = buildQueryWrapper(bo);
        Page<VideoDanmakuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的视频弹幕列表
     *
     * @param bo 查询条件
     * @return 视频弹幕列表
     */
    @Override
    public List<VideoDanmakuVo> queryList(VideoDanmakuBo bo) {
        LambdaQueryWrapper<VideoDanmaku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VideoDanmaku> buildQueryWrapper(VideoDanmakuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VideoDanmaku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getVideoId() != null, VideoDanmaku::getVideoId, bo.getVideoId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), VideoDanmaku::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getColor()), VideoDanmaku::getColor, bo.getColor());
        lqw.eq(bo.getVideoTime() != null, VideoDanmaku::getVideoTime, bo.getVideoTime());
        lqw.eq(bo.getContentPosition() != null, VideoDanmaku::getContentPosition, bo.getContentPosition());
        lqw.orderByAsc(VideoDanmaku::getVideoTime);
        return lqw;
    }

    /**
     * 新增视频弹幕
     *
     * @param bo 视频弹幕
     * @return 是否新增成功
     */
    @Override
    public VideoDanmaku insertByBo(VideoDanmakuBo bo) {
        VideoDanmaku add = MapstructUtils.convert(bo, VideoDanmaku.class);
        validEntityBeforeSave(add);
        add.setContent(sensitiveWordFilter.filterContent(add.getContent()));

        Video video = videoMapper.selectById(bo.getVideoId());
        video.setDanmakuCount(video.getDanmakuCount() + 1);
        videoMapper.updateById(video);

        baseMapper.insert(add);
        return add;
    }

    /**
     * 修改视频弹幕
     *
     * @param bo 视频弹幕
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(VideoDanmakuBo bo) {
        VideoDanmaku update = MapstructUtils.convert(bo, VideoDanmaku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VideoDanmaku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除视频弹幕信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
